package com.ihai.spring.sample.smpp;

import org.jsmpp.InvalidResponseException;
import org.jsmpp.PDUException;
import org.jsmpp.bean.*;
import org.jsmpp.extra.NegativeResponseException;
import org.jsmpp.extra.ResponseTimeoutException;
import org.jsmpp.session.BindParameter;
import org.jsmpp.session.QuerySmResult;
import org.jsmpp.session.SMPPSession;
import org.jsmpp.util.AbsoluteTimeFormatter;
import org.jsmpp.util.TimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

/**
 * DESC
 *
 * @author 张驰
 * @date 2019/11/12
 */
public class SubmitMessageExample {
    private static final Logger LOGGER = LoggerFactory.getLogger(SubmitMessageExample.class);
    private static final TimeFormatter TIME_FORMATTER = new AbsoluteTimeFormatter();;

    public static void main(String[] args) {
        SMPPSession session = new SMPPSession();
        try {
            session.connectAndBind("127.0.0.1", 8888, new BindParameter(BindType.BIND_TX, "test", "test", "logica1", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));

            Random random = new Random();

            final int totalSegments = 3;
            OptionalParameter sarMsgRefNum = OptionalParameters.newSarMsgRefNum((short)random.nextInt());
            OptionalParameter sarTotalSegments = OptionalParameters.newSarTotalSegments(totalSegments);

            for (int i = 0; i < totalSegments; i++) {
                final int seqNum = i + 1;
                String message = "测试测试测试测试Message part " + seqNum + " of " + totalSegments + " ";
                OptionalParameter sarSegmentSeqnum = OptionalParameters.newSarSegmentSeqnum(seqNum);
                String messageId = submitMessage(session, message, sarMsgRefNum, sarSegmentSeqnum, sarTotalSegments);
                LOGGER.info("Message submitted, message_id is {}", messageId);
            }
            session.unbindAndClose();
        } catch (IOException e) {
            LOGGER.error("Failed connect and bind to host", e);
        }
    }

    public static String submitMessage(SMPPSession session, String message, OptionalParameter sarMsgRefNum, OptionalParameter sarSegmentSeqnum, OptionalParameter sarTotalSegments) {
        String messageId = null;
        try {
            messageId = session.submitShortMessage("CMT", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "1616", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "628176504657", new ESMClass(), (byte)0, (byte)1,  TIME_FORMATTER
                    .format(new Date()), null, new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT), (byte)0, DataCodings.ZERO, (byte)0, message.getBytes(), sarMsgRefNum, sarSegmentSeqnum, sarTotalSegments);
            queryMessage(session,messageId);
        } catch (Exception e) {
            LOGGER.error("submit short message error.", e);
        }
        return messageId;
    }

    public static QuerySmResult queryMessage(SMPPSession session, String messageId) {
        QuerySmResult message = null;
        try {
            message = session.queryShortMessage(messageId,TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN,"1616");
            LOGGER.info(messageId + "----" +message.getMessageState());
        } catch (Exception e) {
            LOGGER.error("Query short message error.", e);
        }
        return message;
    }
}
